package megrez.common.core.services;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lombok.Data;
import megrez.common.core.util.MailValidateException;
import megrez.common.core.util.StringHelper;

/**
 * 邮件发送对象
 * 
 * @author Lucky Yang
 * @since 0.0.1
 */
@Data
public class MailSenderBean {
    /** 主题 */
    private String subject;
    /** 收件人 */
    private List<String> mails = new ArrayList<>();
    /** 参数 */
    private Map<String, Object> paramters = new HashMap<>();
    /** 是否文本 */
    private boolean textable;
    /** 内容 */
    private String body;

    public MailSenderBean addParam(String key, String value) {
        paramters.put(key, value);
        return this;
    }

    public MailSenderBean addMail(String mail) {
        if (StringHelper.hasText(mail)) {
            mails.add(mail);
        }
        return this;
    }

    public void validate() {
        MailValidateException exception = new MailValidateException();
        if (mails == null || mails.isEmpty()) {
            throw exception;
        }

        if (!(StringHelper.hasText(subject) && StringHelper.hasText(body))) {
            throw exception;
        }
    }
}
